package com.npjscg.medicalpro.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.npjscg.medicalpro.common.Constants;
import com.npjscg.medicalpro.common.Result;

import com.npjscg.medicalpro.controller.dto.ReportDTO;
import com.npjscg.medicalpro.entity.Report;
import com.npjscg.medicalpro.entity.Template;
import com.npjscg.medicalpro.service.IReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author raote
 * @since 2022-06-29
 */
@Api(tags = "用户上报模块")
@RestController
@RequestMapping("/report")
public class ReportController {

    @Autowired
    private IReportService reportService;


    /*重要！！！！
    * 用户填表需要发送两个请求，一个建立report一个建立check 需要配合async await进行同步请求
    * */
    @ApiOperation(value = "填写报表")
    @PostMapping("/fillForm")
    public Result fillForm(@RequestBody Report report){
        if (report.getDid() == null || report.getHcode() == null)
            return Result.error(Constants.CODE_400,"请重新选择部门和医疗机构！");
        reportService.fillForm(report);
        return Result.success();
    }

    @ApiOperation(value = "重新编辑")
    @PostMapping("/updateForm")
    public Result updateForm(@RequestBody Report report){

        return Result.success(reportService.updateForm(report));
    }

    @ApiOperation(value = "通过报表id获取报表")
    @GetMapping("getById/{id}")
    public Result getById(@PathVariable Integer id){
        Report byId = reportService.getById(id);
        if (id == null)
            return Result.error(Constants.CODE_400,"id为空");
        else if (id < 0) {
            return Result.error(Constants.CODE_600, "id为负数，前端出错");
        }
        else if (byId == null) {
            return Result.error(Constants.CODE_600, "数据库中没有此记录");
        }
        else
            return Result.success(reportService.getDTO(id));
   }
    @ApiOperation(value = "用户选择查询三种状态下的上报表单，0 未审核 1 未通过 2已通过")
    @GetMapping("/getByStatus")
    public Result getByStatus (@RequestParam("pagenum") int pagenum , @RequestParam("pagesize") int pagesize,
    @RequestParam("uid") int uid, @RequestParam("status") int status, @RequestParam(value = "temname",defaultValue = "",required = false) String temname ){
        Page<ReportDTO> page = new Page<>(pagenum,pagesize);
        return Result.success(reportService.getByStatus(page, uid, status, temname));
   }


}
